<?php
/**
 * Created by PhpStorm.
 * User: wzy12
 * Date: 2018/9/20
 * Time: 14:14
 */

namespace app\admin\model;


use think\Model;
use app\admin\validate\GoodsCate as Vali;

class GoodsCate extends Model
{
    public function resList(){
        $list = $this -> order('sort desc') -> select();
        $count = $this -> count();

        $list = cateList($list);
        foreach($list as $k => $v){
            $next = false;
            if(isset($list[$k + 1])){
                $next = $list[$k + 1];
            }

            if($next){
                if($v['level'] + 1 != $next['level']){
                    $list[$k]['max'] = true;
                }
            }else{
                $list[$k]['max'] = true;
            }
        }

        $result['count'] = $count;
        $result['list'] = $list;

        return $result;
    }

    public function operation($data){
        $query = $this -> where('cate_name', $data['cate_name']) -> where('pid', $data['pid']);
        $action = false;
        if(isset($data['id'])){
            if($data['id'] != $data['pid']){
                $action = true;
                $query -> where('id', '<>', $data['id']);
            }else {
                unset($data['id']);
            }
        }

        // 判断是否已有该分类
        $res = $query -> value('id');
        if($res){
            return echoArr(0, '该分类已存在');
        }

        // 验证
        $validate = new Vali();
        if(!$validate -> scene('form') -> check($data)){
            return echoArr(0, $validate->getError());
        }

        $result = $this -> allowField(true) -> isUpdate($action) -> save($data);
        if(false === $result){
            return echoArr(0, $this -> getError());
        }else{
            return echoArr(1, '操作成功');
        }
    }

    public function resFind($data){
        $list = $this -> select();
        $list = cateList($list);
        $result = $this -> find($data);
        $result['list'] = $list;

        return $result;
    }

    public function del($data){
        // 验证
        $validate = new Vali();
        if(!$validate -> scene('del') -> check($data)){
            return echoArr(0, $validate->getError());
        }

        // 判断该分类是否存在商品
        $is_goods = model('Goods') -> whereIn('cate_id', $data['id']) -> value('id');
        if($is_goods){
            return echoArr(0, "商品ID为【{$is_goods}】的是此分类，不允许删除此分类");
        }

        $res = $this -> whereIn('pid', $data['id']) -> value('id');
        $result = false;
        if(!$res){
            $result = $this -> destroy($data['id']);
        }else{
            return echoArr(0, '请将子分类先删除');
        }

        if(false === $result){
            return echoArr(0, '操作失败');
        }else{
            return echoArr(1, '操作成功');
        }
    }

    /**
     * 更改排序
     */
    public function editSort($data){
        // 验证
        $validate = new Vali();
        if(!$validate -> scene('sort') -> check($data)){
            return echoArr(0, $validate->getError());
        }

        $result = $this -> allowField(true) -> isUpdate(true) -> save($data);

        if(false === $result){
            return echoArr(0, $this -> getError());
        }else{
            return echoArr(1, '更新成功');
        }
    }

    /**
     * 更改状态
     */
    public function editStatus($data){
        // 判断该分类是否存在商品
//        $list = $this -> select();
//        $pid = $this -> where('id', $data['id']) -> value('pid');
//        $list = cateFind($list, $pid, $data['id']);
//        foreach($list as $k => $v){
//            $id[] = $v['id'];
//        }
//        $is_goods = model('Goods') -> whereIn('cate_id', $id) -> value('id');
//        if($is_goods){
//            return echoArr(0, "商品ID为【{$is_goods}】的是此分类，不允许关闭此状态");
//        }

        // 验证
        $validate = new Vali();
        if(!$validate -> scene('status') -> check($data)){
            return echoArr(0, $validate->getError());
        }

        $result = $this -> allowField(true) -> isUpdate(true) -> save($data);
        if(false === $result){
            return echoArr(0, $this -> getError());
        }else{
            return echoArr(1, '操作成功');
        }
    }
}